package cn.bytecollege.admin.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.OrderDetail;

import java.util.List;

/**
 * @author NJK
 */
public class OrderDetailDao extends BaseDao<OrderDetail,Integer> {
    /**
     * 根据订单号查询该订单的详情表
     * @author NJK
     * @param orderNumber
     * @return
     */
    public List<OrderDetail> findByOrderNumber(String orderNumber){
        String sql = "SELECT OD.cuisine_id,c.cuisine_name,c.cuisine_price,OD.number,OD.sell_price " +
                "FROM order_detail OD left JOIN cuisine c on OD.cuisine_id =c.cuisine_id " +
                "WHERE OD.order_number =?";
        return helper.executeQuery(sql,OrderDetail.class,orderNumber);
    }

    /**
     * 根据订单号计算该订单的总价
     * @author NJK
     * @param orderNumber
     * @return
     */
    public List<OrderDetail> calculatePriceByOrderNumber(String orderNumber){
        String sql ="SELECT o.order_detail_id,c.cuisine_price,o.number " +
                "FROM order_detail o INNER JOIN cuisine c on o.cuisine_id=c.cuisine_id " +
                "WHERE o.order_number=?";
        return helper.executeQuery(sql,OrderDetail.class,orderNumber);
    }

    /**
     *根据订单详情的Id更改sellPrice
     * * @author NJK
     * @param orderDetail
     * @return
     */
    public int updateSellPriceByOrderDetailId(OrderDetail orderDetail){
        String sql = "UPDATE order_detail SET sell_price = ? WHERE order_detail_id = ?";
        Object[] objects ={orderDetail.getSellPrice(),orderDetail.getOrderDetailId()};
        return helper.executeUpdate(sql,objects);
    }

    /**
     * 将购物车数据添加进订单详情表
     * @author zhy
     * @param orderDetail
     * @return
     */
    @Override
    public int save(OrderDetail orderDetail) {
        String sql = "INSERT INTO ORDER_DETAIL (ORDER_NUMBER,CUISINE_ID,NUMBER,SELL_PRICE) VALUES(?,?,?,?)";
        Object[] objects = {orderDetail.getOrderNumber(),orderDetail.getCuisineId(), orderDetail.getNumber(), orderDetail.getSellPrice()};
        return helper.executeUpdate(sql, objects);
    }

}
